package Experiment_1;

import java.util.Random;  
import java.util.Scanner;  
  
public class GuessNumber {  
    private static final int MIN_VALUE = 0;  
    private static final int MAX_VALUE = 99;  
    private static final int MAX_TRIES = 10;  
    private static int score = 0;  
  
    public static void main(String[] args) {  
        Scanner scanner = new Scanner(System.in);
  
        while (true) {  
            printMenu();  
            int choice = scanner.nextInt();  
  
            if (choice == 1) {  
                playGame(scanner);  
            } else if (choice == 2) {  
                System.out.println("退出游戏。");  
                System.exit(0);  
            } else {  
                System.out.println("无效的选项，请重新选择。");  
            }  
  
            scanner.nextLine(); // 清除输入缓冲区中的换行符  
        }  
    }  
  
    private static void playGame(Scanner scanner) {  
        int numberToGuess = generateRandomNumber();  
        int tries = 0;  
        boolean won = false;  
  
        while (tries < MAX_TRIES && !won) {  
            System.out.println("请输入你猜的数（0~99）：");  
            int guess = scanner.nextInt();  
  
            if (guess == numberToGuess) {  
                won = true;  
                updateScore(tries);  
                System.out.println("你猜对了！得分：" + score);  
            } else if (guess < numberToGuess) {  
                System.out.println("太小了！");  
            } else {  
                System.out.println("太大了！");  
            }  
  
            tries++;  
        }  
  
        if (!won) {  
            System.out.println("很遗憾，你没有猜对。正确答案是：" + numberToGuess);  
            updateScore(MAX_TRIES); // 扣分  
            System.out.println("得分：" + score);  
        }  
  
        printGameOverMenu(scanner);  
    }  
  
    private static int generateRandomNumber() {  
        Random random = new Random();  
        return random.nextInt(MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;  
    }  
  
    private static void updateScore(int tries) {  
        if (tries == 0) {  
            score += 3;  
        } else if (tries == 1) {  
            score += 2;  
        } else if (tries == 2) {  
            score += 1;  
        } else {  
            score -= 2;  
        }  
    }  
  
    private static void printMenu() {  
        System.out.println("1.开始");  
        System.out.println("2.退出");  
    }  
  
    private static void printGameOverMenu(Scanner scanner) {  
        System.out.println("游戏结束，请选择：");  
        System.out.println("1.再来一次");  
        System.out.println("2.退出");  
  
        int choice = scanner.nextInt();  
  
        if (choice == 1) {  
            // 清除输入缓冲区中的换行符  
            scanner.nextLine();  
            playGame(scanner);  
        } else if (choice == 2) {  
            System.out.println("退出游戏。");  
        } else {  
            System.out.println("无效的选项，将退出游戏。");  
            System.exit(0);  
        }  
    }  
}